package com.tbfs.admin.web;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.tbfs.admin.service.sdk.IUserInfoService;
import com.tbfs.admin.vo.UserRole;
import com.tbfs.admin.vo.Userinfo;
import com.tbfs.common.BaseAction;
import com.tbfs.util.DigestUtils;

public class SaveUserInfoAction extends BaseAction implements ModelDriven<Userinfo>{
	
	private static final Logger logger = Logger.getLogger(SaveUserInfoAction.class);
	
	private String user_id;
	private String role_id;
	
	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getRole_id() {
		return role_id;
	}

	public void setRole_id(String role_id) {
		this.role_id = role_id;
	}


	private Userinfo userinfo = new Userinfo();
	

	public Userinfo getUserinfo() {
		return userinfo;
	}

	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}

	
	private String message = null;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
	// init service
	private IUserInfoService userinfoService;
	
	// 构造函数
	public SaveUserInfoAction(IUserInfoService userinfoService) {
		this.userinfoService = userinfoService;
	}

	public SaveUserInfoAction() {

	}
	
	
	/**
	 * save contractinfo data 
	 * @return
	 */
	public String saveData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin save userinfoinfo +++");
		}
		logger.info("userinfoName:"+userinfo.getUsername());
		
		logger.info("id:"+userinfo.getId());
		
		try {
			int id = userinfo.getId()==null?0:userinfo.getId();
			if(id>0){
				userinfoService.modify(userinfo);
				userinfo.setMessage("success");
			}else{
				userinfo.setUserpassword(DigestUtils.md5Hex("123456"));
				userinfoService.add(userinfo);
				userinfo.setMessage("success");
			}
			
			
		} catch (Exception e) {
			userinfo.setMessage("添加用户失败");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++end save contractinfo +++");
		}
		return Action.SUCCESS;
	}
	
	
	/**
	 * save contractinfo data 
	 * @return
	 */
	public String saveUserRoleInfo() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin save saveUserRoleInfo +++");
		}
		UserRole userRole = new UserRole();
		userRole.setUser_id(user_id);
		userRole.setRole_id(role_id);
		try {
			userinfoService.addUserRole(userRole);
			userinfo.setMessage("success");
		} catch (Exception e) {
			userinfo.setMessage("保存失败");
			e.printStackTrace();
		}
		
		return Action.SUCCESS;
	}
	
	
	
	
	public String destoryData() {
		if(logger.isDebugEnabled()){
			logger.debug("+++begin delete contractinfo +++");
		}
		
		logger.info("id:"+userinfo.getId());
		
		try {
		
			userinfoService.destory(userinfo);
			
			userinfo.setMessage("success");
			
		} catch (Exception e) {
			userinfo.setMessage("删除用户");
			e.printStackTrace();
		}
		if(logger.isDebugEnabled()){
			logger.debug("+++end delete contractinfo +++");
		}
		return Action.SUCCESS;
	}

	public Userinfo getModel() {
		// TODO Auto-generated method stub
		return userinfo;
	}

}
